addpath('.\functionsRA')
load('.\output\results_est_101_US_quart_mid.mat')
load('.\input\dists_US.mat','years')
ending = '_US_quart_mid';

Qs_time = Qs;
load('.\output\results_est_1_US.mat','Qs')
Qs = Qs(:); Qs = Qs(Qs>0);
lth0 = size(Qs,1);

f=3;
lth = size(Qs_time,1);
% graph with comparison of the models
p = plot((1:lth0),100*Qs(:,1),(1:lth)*f-1,100*Qs_time(:,1),(1:lth0),100*mean(Qs(:,1)).*ones(lth0,1),'--',(1:lth*f-1),100*mean(Qs_time(:,1)).*ones(lth*f-1,1),'--','LineWidth',2);
p(1).Color = '#A2142F'; p(2).Color = '#0072BD';
p(3).Color = '#A2142F'; p(4).Color = '#0072BD';
title('Result of Minimisations for US (# parameters)')
legend('Model 1 (792)','Model 101, quarterly (135)','Location','northwest')
axis([1 max(lth0,lth*3-1) 0 10.5])
set(gca,'Xtick',(1:12:max(lth0,lth*3)),'Ytick',(0:1:10),'XtickLabel',years,'XTickLabelRotation',45);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
print('-bestfit',['.\figures matlab\100s_comparison' ending '.pdf'],'-dpdf')

mdl = 1;

name = ['model_' num2str(mdl+100) ];
% graph with probabilities for the A matrix over time, begin careful about which parameters are assumed to be constant or not
if mdl == 1
    pis_overtime{mdl,1} = [repmat(pis{mdl,1}(1:3,1),1,lth); reshape(pis{mdl,1}(4:end),3,lth)]; %#ok<*SAGROW>
    plot((1:lth),100*pis_overtime{mdl,1},'LineWidth',2);
    legend('\pi_{L}','\pi_{H}','\pi_{mr}','\pi_{nL,t}','\pi_{nH,t}','\pi_{nn,t}','Location','northwest','NumColumns',2)
end
title(['Probabilities for Model (' num2str(mdl+100) ')'])
axis([1 lth 0 52])
set(gca,'Xtick',(1:4:lth),'Ytick',(0:5:50),'XtickLabel',years,'XTickLabelRotation',45);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
print('-bestfit',['.\figures matlab\' name '' ending '\' name '' ending '_probs.pdf'],'-dpdf')

% graphs with the marginal densities in the model and in the data
xgrid = (1:8)';
xlabels = {'<-1%','-1-0%','0-1%','1-2%','2-3%','3-4%','4-5%','>5%'};
for i = 1:lth
plot(xgrid,100*[fs_data{mdl,1}(:,i) fs_model{mdl,1}(:,i)],'LineWidth',2)
if mod(i,4) == 1
title(['Avg of Margs t+6 to t+10 in Q1/' years{(i+3)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 2
title(['Avg of Margs t+6 to t+10 in Q2/' years{(i+2)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 3
title(['Avg of Margs t+6 to t+10 in Q3/' years{(i+1)/4} ', US, Model (' num2str(mdl+100) ')'])
else
title(['Avg of Margs t+6 to t+10 in Q4/' years{i/4} ', US, Model (' num2str(mdl+100) ')'])
end
legend('Data','Model','Location','northwest')
axis([1 xgrid(end) 0 50])
set(gca,'Xtick',xgrid,'Ytick',(0:5:50),'XtickLabel',xlabels);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
if mod(i,4) == 1
print('-bestfit',['.\figures matlab\' name '' ending '\marginals\' name '' ending '_marginals_' years{(i+3)/4} '_Q1.pdf'],'-dpdf')
elseif mod(i,4) == 2
print('-bestfit',['.\figures matlab\' name '' ending '\marginals\' name '' ending '_marginals_' years{(i+2)/4} '_Q2.pdf'],'-dpdf')
elseif mod(i,4) == 3
print('-bestfit',['.\figures matlab\' name '' ending '\marginals\' name '' ending '_marginals_' years{(i+1)/4} '_Q3.pdf'],'-dpdf')
else
print('-bestfit',['.\figures matlab\' name '' ending '\marginals\' name '' ending '_marginals_' years{i/4} '_Q4.pdf'],'-dpdf')
end
end

% graphs with the ZC t+5 probabilities in the model and in the data
for i = 1:lth
plot(xgrid,100*[gs_data{mdl,1}(:,1,i) gs_model{mdl,1}(:,1,i)],'LineWidth',2)
if mod(i,4) == 1
title(['Dis of Avg Inf t+5 in Q1/' years{(i+3)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 2
title(['Dis of Avg Inf t+5 in Q2/' years{(i+2)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 3
title(['Dis of Avg Inf t+5 in Q3/' years{(i+1)/4} ', US, Model (' num2str(mdl+100) ')'])
else
title(['Dis of Avg Inf t+5 in Q4/' years{i/4} ', US, Model (' num2str(mdl+100) ')'])
end
legend('Data','Model','Location','northwest')
axis([1 xgrid(end) 0 50])
set(gca,'Xtick',xgrid,'Ytick',(0:5:50),'XtickLabel',xlabels);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
if mod(i,4) == 1
print('-bestfit',['.\figures matlab\' name '' ending '\avg5\' name '' ending '_avg5_' years{(i+3)/4} '_Q1.pdf'],'-dpdf')
elseif mod(i,4) == 2
print('-bestfit',['.\figures matlab\' name '' ending '\avg5\' name '' ending '_avg5_' years{(i+2)/4} '_Q2.pdf'],'-dpdf')
elseif mod(i,4) == 3
print('-bestfit',['.\figures matlab\' name '' ending '\avg5\' name '' ending '_avg5_' years{(i+1)/4} '_Q3.pdf'],'-dpdf')
else
print('-bestfit',['.\figures matlab\' name '' ending '\avg5\' name '' ending '_avg5_' years{i/4} '_Q4.pdf'],'-dpdf')
end
end

% graphs with the ZC t+10 probabilities in the model and in the data
for i = 1:lth
plot(xgrid,100*[gs_data{mdl,1}(:,2,i) gs_model{mdl,1}(:,2,i)],'LineWidth',2)
if mod(i,4) == 1
title(['Dis of Avg Inf t+10 in Q1/' years{(i+3)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 2
title(['Dis of Avg Inf t+10 in Q2/' years{(i+2)/4} ', US, Model (' num2str(mdl+100) ')'])
elseif mod(i,4) == 3
title(['Dis of Avg Inf t+10 in Q3/' years{(i+1)/4} ', US, Model (' num2str(mdl+100) ')'])
else
title(['Dis of Avg Inf t+10 in Q4/' years{i/4} ', US, Model (' num2str(mdl+100) ')'])
end
legend('Data','Model','Location','northwest')
axis([1 xgrid(end) 0 50])
set(gca,'Xtick',xgrid,'Ytick',(0:5:50),'XtickLabel',xlabels);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
if mod(i,4) == 1
print('-bestfit',['.\figures matlab\' name '' ending '\avg10\' name '' ending '_avg10_' years{(i+3)/4} '_Q1.pdf'],'-dpdf')
elseif mod(i,4) == 2
print('-bestfit',['.\figures matlab\' name '' ending '\avg10\' name '' ending '_avg10_' years{(i+2)/4} '_Q2.pdf'],'-dpdf')
elseif mod(i,4) == 3
print('-bestfit',['.\figures matlab\' name '' ending '\avg10\' name '' ending '_avg10_' years{(i+1)/4} '_Q3.pdf'],'-dpdf')
else
print('-bestfit',['.\figures matlab\' name '' ending '\avg10\' name '' ending '_avg10_' years{i/4} '_Q4.pdf'],'-dpdf')
end
end

% graph with the prob of average inflation from t+6~t+10 being a disaster
pi_lim = [(-1:5)'; Inf]; pi_bar = [-2; (-0.5:4.5)'; 6];
prob_avg_dis = zeros(lth,2); prob_avg_dis04 = prob_avg_dis; prob_avg_dis04_1to5 = prob_avg_dis; prob_avg_dis_1to5 = prob_avg_dis;
load('.\input\dists_US.mat','infl')
infl = infl(:);

f = 3;
for i = 1:lth
    bin = (pi_lim>=infl(1+(i-1)*f)); bt = find(bin,1);
    x = [pis{mdl,1}(1:3,1); pis{mdl,1}(3*i+1:3*(i+1))];
    A = Amatrix_time(x,mdl);
    g = avg_inf_6to10(A,bt,pi_bar,pi_lim);
    prob_avg_dis(i,:) = [g(1) g(end)];
    prob_avg_dis04(i,:) = [g(1)+g(2) g(end-1)+g(end)];

    g = avg_inf_1to5(A,bt,pi_bar,pi_lim);
    prob_avg_dis_1to5(i,:) = [g(1) g(end)];
    prob_avg_dis04_1to5(i,:) = [g(1)+g(2) g(end-1)+g(end)];
end
plot((1:lth),100*prob_avg_dis,'LineWidth',2)
title(['Prob of Avg Infl t+6~10 is a disaster, US, Model(' num2str(mdl+100) ')'])
legend('Deflation','Hyperinflation','Location','northeast')
axis([1 lth 0 11])
set(gca,'Ytick',(0:1:11),'XtickLabel',years,'XTickLabelRotation',45,'Xtick',(1:4:lth));
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
print('-bestfit',['.\figures matlab\' name '' ending '\' name '' ending '_prob_avgdis.pdf'],'-dpdf')

% graph with the prob of average inflation from t+6~t+10 being a disaster with thresholds of 0 and 4 instead of -1 and 5
plot((1:lth),100*prob_avg_dis04,'LineWidth',2)
title(['Prob of Avg Infl t+6~10 is a disaster, US, Model(' num2str(mdl+100) ')'])
legend('Deflation','Hyperinflation','Location','northwest')
axis([1 lth 0 32.5])
set(gca,'Ytick',(0:2:32),'XtickLabel',years,'XTickLabelRotation',45,'Xtick',(1:4:lth));
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
print('-bestfit',['.\figures matlab\' name '' ending '\' name '' ending '_prob_avgdis04.pdf'],'-dpdf')

% graph with the prob of average inflation from t+1~t+5 being a disaster with thresholds of 0 and 4
plot((1:lth),100*prob_avg_dis04_1to5,'LineWidth',2)
title(['Prob of Avg Infl t+1~t+5 is a disaster, US, Model(' num2str(mdl+100) ')'])
legend('Deflation','Hyperinflation','Location','northwest')
axis([1 lth 0 28])
set(gca,'Xtick',(1:4:lth),'Ytick',(0:2:31),'XtickLabel',years,'XTickLabelRotation',45);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12],'position',[0 0 17 12])
print('-bestfit',['.\figures matlab\' name '' ending '\' name '' ending '_prob_avgdis04_1to5.pdf'],'-dpdf')

% graph on R2 and RMSE
p = plot((1:lth),100*R2(:,1),'-',(1:lth),R2(:,1)*0+100*mean(R2(:,1)),'r--','LineWidth',2);
p(1).Color = '#A2142F';
p(2).Color = '#A2142F';
title(['Model Fit, US, Model(' num2str(100+mdl) ')'])
axis([1 lth 0 100])
set(gca,'Xtick',(1:4:lth),'Ytick',(0:10:100),'XtickLabel',years,'XTickLabelRotation',45);
ax = gca; ax.FontSize = 16; grid on
ax.YAxis.TickLabelFormat = '%g%%';

yyaxis right
p = plot((1:lth),100*Qs_time,'-',(1:lth),Qs_time*0+100*mean(Qs_time),'--','LineWidth',2);
p(1).Color = '#0072BD';
p(2).Color = '#0072BD';
axis([1 lth 0 20]); ax = gca; ax.FontSize = 16; box on; grid on
ax.YAxis(2).Color = [0 0 0]; ax.YAxis(2).TickLabelFormat = '%g%%';
set(gcf,'Units','centimeters','PaperSize',[17 12])
set(gca,'Ytick',(0:2:20),'Units','centimeters','position',[1.8 3 11.3 7.3]); % [left bottom width height])
legend('R2, lhs','mean R2','RMSE, rhs','mean RMSE','Location','southoutside','NumColumns',4,'Units','centimeters','position',[1.45 0.3 12 0.7])
print('-bestfit',['.\figures matlab\' name '' ending '\' name '' ending '_fit.pdf'],'-dpdf')
